<?php

namespace app\controllers;

use app\models\Slide;
use Yii;
use yii\base\Module;
use yii\web\UploadedFile;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\User;
use app\models\Mall;
use app\models\Desk;
use app\models\Message;
use app\models\Upload;


class BackController extends Controller
{
    public $identity;
    public function __construct($id, Module $module, array $config = [])
    {
        parent::__construct($id, $module, $config);
        $this->identity = Yii::$app->user->identity;
        if(is_null($this->identity))
        {
            header('Location:/login');
        }
    }
    #管理员专用操作
    private function adminMeth()
    {
        if($this->identity['type']!='admin')
        {
            header('Location:/login');
        }
    }

    /*店铺 删除操作*/
    public function actionMallDel($id)
    {
        #管理员专用
        $this->adminMeth();

        $request=Yii::$app->request;
        $mall_md=new Mall();
        $mall_md->delOne($id);
        $referer=$request->getReferrer();
        header('Location:'.$referer);

    }

    /*店铺 编辑操作*/
    public function actionMallEdit()
    {
        #管理员专用
        $this->adminMeth();

        $request=Yii::$app->request;
        $id=$request->post('id');
        $password=$request->post('password');
        $name=$request->post('name');
        if(!($name AND $id))
        {
            exit('error');
        }
        #编辑店铺
        $mall_md=new Mall();
        $mall_md->editName($id,$name);
        #新建店铺管理员帐号
        $user_md=new User();
        $user_info=$user_md->getOneByMallId($id);
        if(!($password==''))
        {
            $user_params=array(
                'pass'=>$password,
            );
            $user_md->editUser($user_info['id'],$user_params);
        }

        $referer=$request->getReferrer();
        header('Location:'.$referer);
    }

    /*店铺 新建操作*/
    public function actionMallNew()
    {
        #管理员专用
        $this->adminMeth();

        $request=Yii::$app->request;
        $username=$request->post('username');
        $password=$request->post('password');
        $mallname=$request->post('mallname');
        if(!($username AND $password AND $mallname))
        {
            $re_json=array(
                'status'=>'error'
            );
            exit(json_encode($re_json));
        }
        $mallcode=md5(time().rand(10000,99999).rand(10000,99999));
        $mall_md=new Mall();
        if($mall_md->createNewAll($mallname,$mallcode,$username,$password))
        {
            $re_json=array(
                'status'=>'success',
                'url'=>'/admin/mall-list/'
            );
        }
        else
        {
            $re_json=array(
                'status'=>'error',
            );
        }
        exit(json_encode($re_json));
    }

    /*桌面 删除操作*/
    public function actionDeskDel($id)
    {
        $request=Yii::$app->request;
        $desk_md=new Desk();
        $desk_md->delOne($id);
        $referer=$request->getReferrer();
        header('Location:'.$referer);
    }

    /*桌面 编辑操作*/
    public function actionDeskEdit()
    {
        $desk_md=new Desk();
        $request=Yii::$app->request;
        $id=$request->post('id');
        $name=$request->post('name');
        if(!($id AND $name))
        {
            exit('error');
        }
        $desk_md->editName($id,$name);
        $referer=$request->getReferrer();
        header('Location:'.$referer);
    }

    /*桌面 新建操作*/
    public function actionDeskNew()
    {
        $desk_md=new Desk();
        $request=Yii::$app->request;
        $mall_id=$request->post('mall_id');
        $deskname=$request->post('deskname');
        if(!($mall_id AND $deskname))
        {
            $re_json=array(
                'status'=>'error',
            );
            exit(json_encode($re_json));
        }
        if($desk_md->createNew($mall_id,$deskname))
        {
            $re_json=array(
                'status'=>'success',
                'url'=>'/admin/desk-list/',
            );
        }
        else
        {
            $re_json=array(
                'status'=>'error',
            );
        }
        exit(json_encode($re_json));
    }

    /*留言 删除操作*/
    public function actionMsgDel($id)
    {
        $request=Yii::$app->request;
        $msg_md=new Message();
        $msg_md->delOne($id);
        $referer=$request->getReferrer();
        header('Location:'.$referer);
    }

    /*留言 编辑操作*/
    public function actionMsgEdit()
    {
        $msg_md=new Message();
        $request=Yii::$app->request;
        $id=$request->post('id');
        $list_id=$request->post('list_id');
        $list_page=$request->post('list_page');
        $message=$request->post('message');
        if(!($id AND $message))
        {
            exit('error');
        }
        $msg_md->editName($id,$message);
        $referer='/admin/msg-list/?id='.$list_id.'&page='.$list_page;
        header('Location:'.$referer);
    }

    /*留言 确认通过审核*/
    public function actionMsgConfirm($id)
    {
        $msg_md=new Message();
        $request=Yii::$app->request;
        $msg_md->confirmMsg($id);
        $referer=$request->getReferrer();
        header('Location:'.$referer);
    }

    /*轮播图 修改操作*/
    public function actionSlideEdit()
    {
        $upload_md = new Upload();
        $slide_md=new Slide();
        if (Yii::$app->request->isPost) {

            $upload_md->file = UploadedFile::getInstance($upload_md, 'slide_1');
            if(!is_null($upload_md->file))
            {
                #获取文件名
                $fname_1=$upload_md->upload('slide_1');
                if(!$fname_1)
                {
                    return false;
                }
                $slide_md->autoUpdate('slide_1','/img/'.$fname_1);
            }

            $upload_md->file = UploadedFile::getInstance($upload_md, 'slide_2');
            if(!is_null($upload_md->file))
            {
                #获取文件名
                $fname_2=$upload_md->upload('slide_2');
                if(!$fname_2)
                {
                    return false;
                }
                $slide_md->autoUpdate('slide_2','/img/'.$fname_2);
            }

            $upload_md->file = UploadedFile::getInstance($upload_md, 'slide_3');
            if(!is_null($upload_md->file))
            {
                #获取文件名
                $fname_3=$upload_md->upload('slide_3');
                if(!$fname_3)
                {
                    return false;
                }
                $slide_md->autoUpdate('slide_3','/img/'.$fname_3);
            }

        }
        $referer=Yii::$app->request->getReferrer();
        header('Location:'.$referer);

//        return true;
    }

    public function actionPassChange()
    {
        $user_md=new User();
        $request=Yii::$app->request;
        $new_pass=$request->post('new_pass');
        $old_pass=$request->post('old_pass');
        if(!$new_pass OR !$old_pass)
        {
            $re_json=array(
                'status'=>'error',
            );
            return json_encode($re_json);
        }
        if(md5($old_pass)!=$this->identity['pass'])
        {
            $re_json=array(
                'status'=>'error'
            );
            return json_encode($re_json);
        }
        $id=$this->identity['id'];
        $up_data=array(
            'pass'=>$new_pass,
        );
        if($user_md->editUser($id,$up_data))
        {
            $re_json=array(
                'status'=>'success',
                'url'=>'/login'
            );
            return json_encode($re_json);

        }
        else
        {
            $re_json=array(
                'status'=>'error'
            );
            return json_encode($re_json);

        }
    }
}
